package Atop100.subArray;

import java.util.HashMap;

public class Class560 {

        public int subarraySum(int[] nums, int k) {
            int count = 0, pre = 0;
            HashMap < Integer, Integer > mp = new HashMap< >();
            mp.put(0, 1);
            for (int i = 0; i < nums.length; i++) {
                pre += nums[i];
                if (mp.containsKey(pre - k)) {
                    count += mp.get(pre - k);
                }
                mp.put(pre, mp.getOrDefault(pre, 0) + 1);
            }
            return count;
        }


        public int subarraySum1(int[] nums, int k) {
            int count = 0;
            for (int start = 0; start < nums.length; ++start) {
                int sum = 0;
                for (int end = start; end >= 0; --end) {
                    sum += nums[end];
                    if (sum == k) {
                        count++;
                    }
                }
            }
            return count;
        }

    public static void main(String[] args) {
        Class560 class560 = new Class560();
        int[] arr={3,4,7,2,-3,1,4,2};
        System.out.println(class560.subarraySum(arr, 7));


    }





}
